﻿namespace AnyOffice.DAL
{
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class RmsCodeDao
    {
        public string GetCodeName(SqlConnection conn, string codeId, CodeType codeType)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@CodeId", SqlDbType.Int) };
            commandParameters[0].Value = Convert.ToInt32(codeId);
            string commandText = "SELECT [CodeName] FROM [dbo].[Sys_Code] WHERE [CodeId] = @CodeId ";
            if (codeType == CodeType.Secret)
            {
                commandText = commandText + " AND [CodeFlag]='RMS_SECRET'";
            }
            if (codeType == CodeType.CertificateKind)
            {
                commandText = commandText + " AND [CodeFlag]='RMS_CERTIFICATE_KIND'";
            }
            if (codeType == CodeType.Urgency)
            {
                commandText = commandText + " AND [CodeFlag]='RMS_URGENCY'";
            }
            if (codeType == CodeType.FileType)
            {
                commandText = commandText + " AND [CodeFlag]='RMS_FILE_TYPE'";
            }
            if (codeType == CodeType.FileKind)
            {
                commandText = commandText + " AND [CodeFlag]='RMS_FILE_KIND'";
            }
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 != null)
            {
                return obj2.ToString();
            }
            return string.Empty;
        }

        public DataTable SelectAll(SqlConnection conn, CodeType codeType)
        {
            string commandText = "SELECT [CodeId],[CodeName] FROM [dbo].[Sys_Code] WHERE [CodeFlag] = '";
            switch (codeType)
            {
                case CodeType.Secret:
                    commandText = commandText + "RMS_SECRET";
                    break;

                case CodeType.CertificateKind:
                    commandText = commandText + "RMS_CERTIFICATE_KIND";
                    break;

                case CodeType.Urgency:
                    commandText = commandText + "RMS_URGENCY";
                    break;

                case CodeType.FileType:
                    commandText = commandText + "RMS_FILE_TYPE";
                    break;

                case CodeType.FileKind:
                    commandText = commandText + "RMS_FILE_KIND";
                    break;
            }
            commandText = commandText + "' ORDER BY [CodeNo] ASC";
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText);
            set.Tables[0].TableName = "Sys_Code";
            return set.Tables[0];
        }

        public enum CodeType
        {
            Secret,
            CertificateKind,
            Urgency,
            FileType,
            FileKind
        }
    }
}

